package com.phone.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.phone.dao.ProductDao;
import com.phone.entity.CartItml;
import com.phone.entity.Product;
import com.phone.entity.Store;
@Repository
public class ProdcutDaoimpl implements ProductDao {
	@Autowired
    private SessionFactory sessionFactory;
	@Override
	public List<Product> allproduct() {
		String hql="from Product";
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		List<Product> listproduct=query.list();
		return listproduct;
	}
	@Override
	public Product seleproduct(Integer id) {
//		Product product=null;
//		String hql="from Product where id=?";
//		try {
//			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
//			query.setParameter(0, id);
//			product=(Product) query.uniqueResult();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return product;
		try {
			Product product = this.sessionFactory.getCurrentSession().load(Product.class, id);
			return product;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@Override
	public void upproduct(Product product) {
		try {
			this.sessionFactory.getCurrentSession().update(product);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@Override
	public boolean delproduct(Integer id) {
		Product product=null;
		String hql="from Product where id=?";
		try {
			Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
			query.setParameter(0, id);
			product=(Product) query.uniqueResult();
			this.sessionFactory.getCurrentSession().delete(product);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

}
